package com.crack.nga2.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * Created by Administrator on 2015/12/2.
 */
public class TimeUtils {
    public static SimpleDateFormat sdf;
    private static SimpleDateFormat sdf_1;

    public static String formatStandardTime(long time) {
        if (sdf_1 == null) {
            sdf_1 = new SimpleDateFormat("yy-M-dd hh:mm");
            sdf_1.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        }
        return sdf_1.format(new Date(time));
    }


    public static String format(String time) {
        if (sdf == null) {
            sdf = new SimpleDateFormat("yy-M-dd");
            sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        }
        try {
            long time1 = sdf.parse(time).getTime();
            return format(time1);
        } catch (ParseException e) {
            e.printStackTrace();
            return "未知";
        }
    }

    /**
     * 日期工具类
     * 将时间转换为：几分钟前、几小时前、昨天、几年几月几日
     */
    public static String format(Long time) {
        if (sdf == null) {
            sdf = new SimpleDateFormat("yy-M-dd");
            sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        }
        Calendar c1 = Calendar.getInstance();
        c1.setTimeInMillis(time);
        int day1 = c1.get(Calendar.DAY_OF_YEAR);
        int h1 = c1.get(Calendar.HOUR_OF_DAY);
        int m1 = c1.get(Calendar.MINUTE);
        Calendar c2 = Calendar.getInstance();
        c2.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        int day2 = c2.get(Calendar.DAY_OF_YEAR);
        int h2 = c2.get(Calendar.HOUR_OF_DAY);
        int m2 = c2.get(Calendar.MINUTE);
        if (day1 == day2) {
            //同一天
            if (h1 == h2) {
                return m2 - m1 <= 0 ? "刚刚" : (m2 - m1) + "分钟前";
            } else {
                return h2 - h1 + "小时前";
            }
        } else {
            sdf.format(new Date(time));
        }
        return sdf.format(time);
    }

}
